package com.minivision.fdiot.service;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;

import com.minivision.fdiot.common.ChunkRequest;
import com.minivision.fdiot.common.CommonConstants;
import com.minivision.fdiot.entity.OpLog;
import com.minivision.fdiot.repository.mysql.OpLogRepository;
import com.minivision.fdiot.rest.param.OpLogParam;

@Service
public class OpLogServiceImpl extends BaseService implements OpLogService {

  @Autowired
  private OpLogRepository opLogRepo;

  @Override
  public OpLog create(OpLog opLog) {
    return opLogRepo.save(opLog);
  }

  @Override
  public Page<OpLog> findByCreateTimeBetween(OpLogParam param) throws Exception {
    SimpleDateFormat sdf = new SimpleDateFormat(CommonConstants.FULL_DATE_FORMAT);
    Date startDate = sdf.parse(param.getStartTime());
    Date endDate = sdf.parse(param.getEndTime());
    return opLogRepo.findByCreateTimeBetweenOrderByCreateTimeDesc(startDate, endDate, new ChunkRequest(param.getOffset(), param.getLimit()));
  }

}
